<template>
	<view class="wrap">
		<u-toast ref="uToast" />
		<u-back-top :scroll-top="scrollTop"></u-back-top>
		<u-tabs :list="list" :is-scroll="false" height="100" bg-color="#3b3b43" active-color='#ffb23f'
			inactive-color='#ffffff' :current="current" @change="change">
		</u-tabs>
		<!-- 动态 -->
		<view class="comment" v-for="(res, index) in commentList">
			<view class="right">
				<view style="display: flex;margin-left: 15rpx;margin-top: 15rpx;">
					<u-avatar @click="userinfo(res)" :src="res.userPic || res.pic"></u-avatar>
					<view style="flex: 1;margin-left: 20rpx;display: flex;flex-direction: column;">
						<view style="    display: flex;     align-items: center;">
							{{res.userName || res.nickName}}

						</view>
						<view style="font-size: 30rpx;">
							<text v-if="res.sex == 'F'">
								女 ·
							</text>
							<text v-if="res.sex == 'M'">
								男 ·
							</text>
							{{res.age}}岁
						</view>
						<!-- <view style="display: flex;">
							<view>{{res.pubDate}}</view>
						</view> -->
					</view>

				</view>
				<!-- 文本内容 -->
				<view class="content">
					{{res.content}}
				</view>
				<!-- 图片显示区域 -->
				<view class="reply-box" v-if="res.pics.length != 0">
					<view v-if="res.pics.length == 1"
						style="padding: 20rpx;display: flex;justify-content: space-between;flex-wrap: wrap;">
						<image @click="previewImage(index1,res.pics)" v-for="(res1, index1) in res.pics" :src="res1"
							mode="aspectFill" style="width: 100%;"></image>
					</view>
					<view v-if="res.pics.length == 2 || res.pics.length == 4"
						style="padding: 20rpx;display: flex;justify-content: space-between;flex-wrap: wrap;">
						<image @click="previewImage(index1,res.pics)" v-for="(res1, index1) in res.pics" :src="res1"
							mode="aspectFill" style="width:334rpx;height:334rpx;margin-bottom: 10rpx;"></image>
					</view>
					<view v-if="res.pics.length == 3 || res.pics.length == 6 || res.pics.length == 7"
						style="padding: 20rpx;display: flex;justify-content: space-between;flex-wrap: wrap;">
						<image @click="previewImage(index1,res.pics)" v-for="(res1, index1) in res.pics" :src="res1"
							mode="aspectFill" style="width:220rpx;height:220rpx;margin-bottom: 10rpx;"></image>
					</view>
					<view v-if="res.pics.length == 5 || res.pics.length == 8"
						style="padding: 20rpx;display: flex;justify-content: space-between;flex-wrap: wrap;">
						<image @click="previewImage(index1,res.pics)" v-for="(res1, index1) in res.pics" :src="res1"
							mode="aspectFill" style="width:220rpx;height:220rpx;margin-bottom: 10rpx;"></image>
						<view style="width:220rpx;height:220rpx;"></view>
					</view>
					<view v-if="res.pics.length == 9" 11
						style="padding: 20rpx;display: flex;justify-content: space-between;flex-wrap: wrap;">
						<image @click="previewImage(index1,res.pics)" v-for="(res1, index1) in res.pics" :src="res1"
							mode="aspectFill" style="margin-bottom: 10rpx;width:220rpx;height:220rpx;"></image>
					</view>
					<view class="like" @click="getLike(res.id,res.isLiked)">
						<view>
							<image
								:src="res.isLiked==0?'../../static/dongtai-like01.png':'../../static/dongtai-like.png'"
								style="width: 100%; height: 100%;" mode="" />
						</view>
						<view>
							喜欢
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="ppp"></view>
		<!-- 关注 -->

		<u-loadmore :status="status" />

	</view>
</template>

<script>
	//import api from '@/api/api.js'
	import * as api from '@/api/api.js'
	export default {
		data() {
			return {
				//总条数
				topiccount: '',
				followtopiccount: '',
				status: 'loadmore',
				isLike: false,
				isSc: false,
				u_id: '',
				Page: '1',
				Fpage: '1',
				urls: [],
				windowswidth: '',
				newwidth: '',
				two: '',
				scrollTop: 0,
				commentList: "",
				commentListFollow: [{
					id: 3,
					uava: "",
					privacy: "公开",
					uname: "测试文字",
					ugender: "男",
					pubDate: "123",
					place: "123",
					repDate: "12:12",
					pics: [
						"https://hbimg.huabanimg.com/b90fa94f6218e6aa6b66b67291bdaec69053051258d5d-GiZ3gi_fw658/format/webp",
						"https://hbimg.huabanimg.com/b90fa94f6218e6aa6b66b67291bdaec69053051258d5d-GiZ3gi_fw658/format/webp",
						"https://hbimg.huabanimg.com/b90fa94f6218e6aa6b66b67291bdaec69053051258d5d-GiZ3gi_fw658/format/webp"
					],
					comNum: "comnum",
					likeNum: "likeNum"
				}],
				list: [{
						name: '全部'
					},
					{
						name: '附近'
					}

				],
				flag: true,
				flag2: true,
				current: 0,
				noClick: true,
				datalist: "",
				arrpp: ""
			}
		},
		onLoad() {
			//获取动态列表
			this.init();
			api.request({
				url: '/p/main',
				method: 'GET',
				data: {},
			}).then(res => {
				for (let item in res.data) {
					if (res.data[item].userLabels == null) {
						console.log("空");

					} else {
						res.data[item].userLabels = res.data[item].userLabels.split(',')

					}
				}
				this.datalist = res.data
				this.datalist.forEach((item, index, arrr) => {
					let arr = {}
					// console.log(item.userLabels,index)
					if (index == 5) {
						console.log(item)
					}
					if (item.age == null) {
						arr.age = 18
					} else {
						arr.age = item.age
					}
					arr.height = item.height
					console.log(item)
					arr.sex = item.sex
					let oop = "2021-07-07 17:22:29"
					var uew = new Date(Date.parse(item.userRegtime.replace(/-/g, "/")));
					var uew1 = new Date(Date.parse(oop.replace(/-/g, "/")))
					if (uew.getTime() >= uew1.getTime()) {
						let arr1 = JSON.parse(item.userLabels)
						arr.cityId = arr1
						console.log(arr1)
						arr.profession = arr1.profession
						arr.province = item.province
						arr.synopsis = arr1.synopsis
					}
					if (item.weight == null) {
						arr.weight = 96
					} else {
						arr.weight = item.weight
					}
					this.datalist[index].areaId = arr
				})
				console.log(this.datalist)
				console.log(78952626)
				api.request({
					url: '/p/unlock',
					method: 'GET',
				}).then(set => {
					this.arrpp = set.data
					console.log(this.arrpp)
				})
			})
		},
		onShow() {
			this.init();
		},
		onPullDownRefresh: function() {
			this.Page = '1'
			this.Fpage = '1'
			//	this.init();
			//  this.getComment();  列表信息值
			//	this.get();
		},
		//触底事件
		onReachBottom() {

		},
		//监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
		onPullDownRefresh() {
			this.init();
			console.log(178551)
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止下拉刷新动画
			}, 1000);
		},
		//返回顶层
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			init() {
				api.request({
					url: '/p/post-record/page',
					method: 'GET',
				}).then(res => {
					for (let item in res.data.records) {
						console.log(res.data.records[item].userPic)
						res.data.records[item].pics = res.data.records[item].pics.split(',')
						// res.data.records[item].userPic = 
					}
					this.commentList = res.data.records
					console.log(this.commentList);
				})
			},
			//取消点赞
			unLike(res) {
				//console.log('取消点赞')
				let t_id = res
				api.request({
					url: '/dianzan/cancelDianzan',
					method: 'POST',
					data: {
						tid: t_id,
						uid: this.u_id
					},
				}).then(res => {
					if (res.data.data == 1) {
						this.getComment();
						// this.$refs.uToast.show({
						// 	title: '取消点赞',
						// 	type: 'warning',
						// 	duration: '1000'
						// })
					}
				})
			},
			//点赞
			getLike(id, type) {
				console.log(type)
				if (type == 0) {
					api.request({
						url: '/p/post-record/like/' + id,
						method: 'POST'
					}).then(res => {

						this.$refs.uToast.show({
							title: '点赞成功',
							type: 'success',
							duration: '1000',
						})
						this.init();
					});
				} else {
					api.request({
						url: '/p/post-record/canceLike/' + id,
						method: 'POST'
					}).then(res => {
						this.$refs.uToast.show({
							title: '取消点赞',
							type: 'success',
							duration: '1000'
						})
						this.init();
					});
				}

			},
			//标签切换
			change(index) {
				this.current = index;
				//this.status = 'loading';
				if (this.current == 1) {
					let a
					console.log(1)
					api.request({
						url: '/p/post-record/nea',
						method: 'GET',
					}).then(res => {
						// console.log(res.data[2].pics.split(','))
						a = res.data
						for (let item of a) {
							console.log(item.pics.split(','))
							// for (let item of )
							item.pics = item.pics.split(',')
						}
						this.commentList = a
						console.log(this.commentList)
						this.followtopiccount = res.data.length;
					})
				}
				if (this.current == 0) {
					let a
					console.log(0)
					api.request({
						url: '/p/post-record/page',
						method: 'GET',
					}).then(res => {
						a = res.data.records
						for (let item of a) {
							item.pics = item.pics.split(',')
							console.log(item.pics.length)
						}
						this.commentList = a
						console.log(this.commentList)
						this.followtopiccount = res.data.total;

					})
				}
			},
			//图片预览
			previewImage(current, photos) {
				console.log(current, photos)
				// const urls = 
				uni.previewImage({
					current,
					urls: photos
				})
			},
			//个人中心
			userinfo(res) {
				if(res.fromUserId == undefined){
					uni.switchTab({
						url:'../user/user'
					})
				}
				let c
				//console.log(res.uid+"页面跳转过来的值")
				// uni.navigateTo({
				// 	url: '../user/userinfo/userinfo?uid=' + res.uid
				// })
				let likelist
				// console.log(res,this.commentList,this.datalist)
				likelist = this.commentList
				for (let a = 0; a < likelist.length; a++) {
					console.log(likelist[a].fromUserId, 1, res.fromUserId)
					if (likelist[a].userId == res.userId) {
						this.datalist.forEach((arr, index) => {
							// console.log(1)
							// unlockedUserId
							if (arr.userId == likelist[a].userId) {
								0
								// console.log(likelist[a].fromUserId,arr.userId)
								c = {
									id: arr.userId,
									vip: arr.isVip,
									dad: arr.areaId,
									ck: 1
								}
							}
						})
					}
				}
				uni.setStorage({
					key: 'daad',
					data: c.dad,
					success: function() {
						// console.log('success');
					}
				});
				for (var item of this.commentList) {
					console.log(item.userId, c.id)
					if (item.userId == c.id) {
						c.ck = 1
						break
					} else {
						c.ck = 0
					}
				}
				console.log(c.dad)
				this.$u.route("pages/user/userinfo/userinfo", {
					id: c.id,
					vip: c.vip,
					ck: c.ck
				})
			},

			//评论详情
			comment(res) {

				uni.navigateTo({
					url: './comment?id=' + res.id + '&name=' + res.uname
				})
			},
			//信息初始化			
			getComment() {

				//关注页面
				if (this.current == 1) {
					api.request({
						url: '/p/post-record/page',
						method: 'GET',

					}).then(res => {
						for (let item in res.data) {
							res.data[item].userLabels = res.data[item].userLabels.split(',')
						}
						this.commentList = res.data.records
						this.followtopiccount = res.data.total

					})
					//动态页面
				} else if (this.current == 0) {

					api.request({
						url: '/p/post-record/page',
						method: 'GET',
					}).then(res => {
						console.log(res.data)
						for (let item in res.data) {
							res.data[item].userLabels = res.data[item].userLabels.split(',')
						}
						this.commentList = res.data.records;
						this.followtopiccount = res.data.total;

					})
				}

				/* 		uni.showLoading({
							title: '加载中'
						}) */

			}

		}
	}
</script>


<style lang="scss" scoped>
	page {
		background-color: #3b3b43;
	}

	.like {
		width: 90%;
		margin: 0 auto;
		display: flex;
	}

	.like>view {
		width: 50rpx;
		height: 50rpx;
	}

	.like>view:nth-child(2) {
		width: 80rpx;
		height: 50rpx;
		margin-left: 20rpx;
	}

	.num {
		display: inline;
	}

	.wrap {
		height: 120%;
		width: 100%;
		background-color: rgb(59, 59, 67);
	}

	.ppp {
		width: 100%;
		height: 100rpx;
	}

	.comment {
		margin-top: 15rpx;
		background: #ffffff;
		display: flex;
		border-radius: 20rpx;
		//padding: 30rpx;
		//margin-top: 10rpx;
		margin-bottom: 20rpx;
		margin-left: 15rpx;
		margin-right: 15rpx;

		.grid-text {
			font-size: 28rpx;
			margin-top: 4rpx;
			color: #2979FF;
		}

		.pic {
			.img_wrapper {
				width: 220upx;
				height: 150upx;
				margin-right: 15upx;
				display: inline-block;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #3b3b43;
			}
		}

		.right {
			flex: 1;
			//padding-left: 20rpx;
			font-size: 30rpx;
			background-color: #3b3b43;
			color: #FFFFFF;

			.gd {

				.image {
					margin-left: 20rpx;
				}
			}

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.name {
					position: absolute;
					left: 110rpx;

				}

				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;

					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}

				.highlight {
					background-color: #3b3b43;
					color: #5677fc;

					.num {
						color: #5677fc;
					}
				}
			}

			.content {
				word-wrap: break-word;
				width: 700rpx;
				margin-right: 10rpx;
				margin-left: 20rpx;
				margin-top: 15rpx;
				margin-bottom: 15rpx;
			}

			.reply-box {
				//background-color: rgb(242, 242, 242);
				//border-radius: 12rpx;

				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;

					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}

				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;

					.more {
						margin-left: 6rpx;
					}
				}
			}

			.test {
				float: left;
				padding: 15rpx;
			}

			.img {
				display: flex;
			}

			.bottom {
				//margin-top: 20rpx;
				// margin-left: 10rpx;
				// display: flex;
				// font-size: 24rpx;
				// color: #9a9a9a;
				// margin-bottom: 10rpx;

				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}

				.weibo-handle {
					display: flex;
					//padding-top: 15rpx;
					font-size: 28rpx;
					padding-bottom: 25rpx;
				}

				.weibo-handle image {
					width: 50rpx;
					height: 50rpx;
					margin-right: 20rpx;
				}

				.weibo-handle view {
					flex: 1;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.weibo-handle .comment-group {
					background: #ffffff;
				}

				.weibo-handle .praise-group {
					background: #ffffff;
				}
			}
		}
	}
</style>
